package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.common.dto.ServiceProcessDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.SeeServiceAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看服务进程
 * weishixiong
 * 2019-03-24
 */
public class SeeServiceActivity extends BaseActivity implements CustomItemClickListener {
    private TextView tvNumber;
    private TextView tvType;
    private TextView tvName;
    private TextView tvDate;
    private TextView tvAdress;
    private RecyclerView recyclerview;
    private SeeServiceAdapter adapter;
    private List<ServiceProcessDto> datas;
    private LinearLayoutManager linearLayoutManager;
    private OrderDto orderDto;
    private OrderViewModel orderViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_seeservice;
    }

    @Override
    public void initView() {
        setNormalTitle("查看服务进程", v -> finish());
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvType = (TextView) findViewById(R.id.tv_type);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvDate = (TextView) findViewById(R.id.tv_date);
        tvAdress = (TextView) findViewById(R.id.tv_adress);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter = new SeeServiceAdapter(this, datas, this);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        orderDto = (OrderDto) getIntent().getSerializableExtra("order");
        if (EmptyUtils.isNotEmpty(orderDto)) {
            tvNumber.setText(orderDto.getOrderNo());
            tvType.setText(orderDto.getServiceType());
            tvName.setText(orderDto.getCustomerName());
            tvDate.setText(orderDto.getBookTime());
            tvAdress.setText(orderDto.getServiceAddress());
            serviceProcess(orderDto.getId());

        }

    }

    /**
     * 查看服务进程
     *
     * @param id
     */
    private void serviceProcess(String id) {
        showLoadingDialog();
        orderViewModel.serviceProcess(id);
        orderViewModel.getServiceProcessliveData().observe(this, new Observer<BaseDto<List<ServiceProcessDto>>>() {
            @Override
            public void onChanged(@Nullable BaseDto<List<ServiceProcessDto>> listBaseDto) {
                hideLoadingDialog();
                if (listBaseDto.isSuccess()) {
                    if (EmptyUtils.isNotEmpty(listBaseDto.getData())) {
                        datas.clear();
                        datas.addAll(listBaseDto.getData());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUitl.showImageToastTips(listBaseDto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
